package neusoft.edu.cn.guanshangshiguang.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import neusoft.edu.cn.guanshangshiguang.Listener.Listener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.bean.RegisterBean;
import neusoft.edu.cn.guanshangshiguang.model.RegisterModel;


public class RegisterActivity extends AppCompatActivity {

    EditText zhanghao,username,password,password1,sex,school,school_id,zhuanye;
    private String email="";
    private String upic="";
    Button button;

    Listener registerListener = new Listener() {
        @Override
        public void onResponse(Object o) {
            RegisterBean registerBean = (RegisterBean) o;
            System.out.println("uid:"+registerBean.getUid());
            if(registerBean.getCode()==1){
                final Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
            }else{
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(RegisterActivity.this, "注册获取信息失败", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String x1=zhanghao.getText().toString();
                String x2=username.getText().toString();
                String x3=password.getText().toString();
                String x4=password1.getText().toString();
                String x5=sex.getText().toString();
                String x6=school.getText().toString();
                String x7=school_id.getText().toString();
                String x8=zhuanye.getText().toString();
                if(x1.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入账号...", Toast.LENGTH_SHORT).show();
                else if(x2.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入用户名...", Toast.LENGTH_SHORT).show();
                else if(x3.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入密码...", Toast.LENGTH_SHORT).show();
                else if(x4.equals(""))
                    Toast.makeText(RegisterActivity.this, "请再次输入密码...", Toast.LENGTH_SHORT).show();
                else if(x5.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入性别...", Toast.LENGTH_SHORT).show();
                else if(x6.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入学校...", Toast.LENGTH_SHORT).show();
                else if(x7.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入学号...", Toast.LENGTH_SHORT).show();
                else if(x8.equals(""))
                    Toast.makeText(RegisterActivity.this, "请输入所在专业...", Toast.LENGTH_SHORT).show();
                else if(!x3.equals(x4)){
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致，请核对...", Toast.LENGTH_SHORT).show();
                    System.out.println("sss:"+x3+"aaa:"+x4);
                }
                else{
                    RegisterModel registerModel = new RegisterModel();
                    System.out.println("456:"+x1+x2+x3+x5+upic+email+x6+x7+x8);
                    registerModel.getResultList(x1,x2,x3,x5,x6,x7,x8,registerListener);
                }
            }
        });
    }

    private void init() {
        zhanghao=findViewById(R.id.editText5);
        username=findViewById(R.id.editText6);
        password=findViewById(R.id.editText7);
        password1=findViewById(R.id.editText8);
        sex=findViewById(R.id.editText9);
        school=findViewById(R.id.editText10);
        school_id=findViewById(R.id.editText11);
        zhuanye=findViewById(R.id.editText12);
        button = findViewById(R.id.button2);
    }
}
